package com.china317.module.gis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.china317.hibernate.HibernateUtil;
import com.china317.module.PageWrapper;


public class GeometryVectorManager {
	private static Log log = LogFactory.getLog( GeometryVectorManager.class);
	
    public void insert(Session session,GeometryVector entity)throws Exception{
			session.save(entity);
	}
	
	public void update(Session session,GeometryVector entity)throws Exception{
			session.saveOrUpdate(entity);
	}
	public List list(Session session,PageWrapper page) {
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.list");
			query.setFirstResult( (page.getCurrentPage() - 1) * page.getPageSize());
			query.setMaxResults(page.getPageSize());
			List list = query.list();
			page.setTotalSize(this.count( session));
			page.setTotalPage( (int) Math.ceil( ( (double) page.getTotalSize()) / page.getPageSize()));
		return list;
	} 
	
	public List<GeometryVector> list(Session session) {
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.list");
			List<GeometryVector> list= query.list();
			return list;
	} 
	
	public List<GeometryVector> listNoRoute(Session session) {
			String sql = "SELECT T1.* FROM YD_GEOMETRY_VECTOR T1 , YD_GEOMETRY T2" +
					" WHERE T1.GEOMETRY_ID = T2.ID(+) AND T2.BUSINESS_TYPE != 'route'" +
					" ORDER BY T1.GEOMETRY_ID, T1.ORDER_CODE";
			List<GeometryVector> list = session.createSQLQuery(sql).addEntity(GeometryVector.class).list();
		return list;
	} 
	
	public List<GeometryVector> findByKey(Session session,String geometryId){
		final Integer code = 1;
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.find");
			query.setParameter("geometryId", geometryId);
			query.setParameter("orderCode", code);
			List<GeometryVector> list= query.list();
			return list;
	}
	
	public List<GeometryVector> findByKeyPolygon(Session session,String geometryId){
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.findPolygon");
			query.setParameter("geometryId", geometryId);
			List<GeometryVector> vector=query.list();
			return vector;
			
	}
	public int count(Session session) {
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.count");
			int count = ((Long)query.uniqueResult()).intValue() ;
		return count;

	}
	
	public List listByGeometry(Session session,String geometryId) {
			Query query = session.getNamedQuery("com.china317.module.gis.GeometryVector.listByGeometry");
			query.setParameter(0, geometryId);
			List list = query.list();
		return list;
	}

	public void insert(Session session,List<GeometryVector> geometryVectors) throws Exception {
    	Connection conn = null;
    	PreparedStatement ps = null;
		try {
			conn = session.connection();
			ps = conn.prepareStatement("insert  into YD_GEOMETRY_VECTOR values(?,?,?,?,null,null,null)");
			for(GeometryVector entity:geometryVectors){
				ps.setString(1, entity.getGeometryId());
				ps.setInt(2, entity.getOrderCode());
				ps.setFloat(3, entity.getLongitude());
				ps.setFloat(4, entity.getLatitude());
				ps.addBatch();
			}
			ps.executeBatch();
		}catch(Exception e){
			throw e;
		} finally {
			if(ps!=null){
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		}
		
	}
}
